import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import PersonalPage from './Personal_Page'
import Login from './User/login'
import Reg from './User/reg'
import Client from './Customer_management/index'
import Employee from './Employee/index'

Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/views/Inedx/index.vue')
  },
  // 登录
  Login,
  Reg,
  // 个人主页
  PersonalPage,
  Client,
  Employee
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('token') && (to.name !== 'reg') && (to.name !== 'login')) next({ name: 'login' })
  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})

export default router
